define(['jquery', 'bootstrap', 'backend', 'addtabs', 'table', 'echarts', 'echarts-theme', 'template','form'], function ($, undefined, Backend, Datatable, Table, Echarts, undefined, Template,Form) {
    var Controller = {
        index: function () {
            var myChart = Echarts.init(document.getElementById('echart'), 'walden');
            var myChart_uv = Echarts.init(document.getElementById('echart_uv'), 'walden');
            var myChart_pay = Echarts.init(document.getElementById('echart_pay'), 'walden');
            var myChart_user = Echarts.init(document.getElementById('echart_user'), 'walden');
            var option = {

                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: [__('Sales'), __('Orders')]
                },
                /*toolbox: {
                    show: true,
                    feature: {
                        dataView: {show: true, readOnly: false},
                        magicType: {show: true, type: ['line', 'bar']},
                        restore: {show: true},
                        saveAsImage: {show: true}
                    }
                },*/
                calculable: true,
                xAxis: [
                    {
                        type: 'category',
                        data: Config.statistics.columns
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: __('Sales'),
                        type: 'bar',
                        data: Config.statistics.paylist,
                        markPoint: {
                            data: [
                                {type: 'max', name: '最大值'},
                                {type: 'min', name: '最小值'}
                            ]
                        },

                    },
                    {
                        name: __('Orders'),
                        type: 'bar',
                        data: Config.statistics.createlist,

                    }
                ]
            };
            var option_uv = {

                tooltip: {
                    trigger: 'item'
                },
                /*legend: {
                    orient: 'vertical',
                    left: 'center',
                },*/
                series: [
                    {
                        name: '访问来源',
                        type: 'pie',
                        radius: '50%',
                        data: [
                            {value: Config.statistics.uv.pc_uv, name: 'PC'},
                            {value: Config.statistics.uv.wap_uv, name: 'WAP'},

                        ],
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
            var option_pay = {
                title: {text: '', subtext: ''},
                tooltip: {trigger: 'axis'},
                legend: {data: ['订单数量','订单金额']},
                toolbox: {
                    show: false,
                    feature: {magicType: {show: true, type: ['stack', 'tiled']}, saveAsImage: {show: true}}
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: Config.statistics.columns
                },
                yAxis: {},
                grid: [{left: 'left', top: 'top', right: '10', bottom: 30}],
                series: [{
                    name: '订单数量',
                    type: 'line',
                    smooth: true,
                    areaStyle: {normal: {}},
                    lineStyle: {normal: {width: 1.5}},
                    data: Config.statistics.createlist
                }, {
                    name: '订单金额',
                    type: 'line',
                    smooth: true,
                    areaStyle: {normal: {}},
                    lineStyle: {normal: {width: 1.5}},
                    data: Config.statistics.orderlist
                }]
            };
            var option_user = {

                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    data: ['新增客户', '订单转化率']
                },
                grid: {
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                toolbox: {
                    feature: {
                        saveAsImage: {}
                    }
                },
                xAxis: {
                    type: 'category',
                    boundaryGap: false,
                    data: Config.statistics.columns
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        name: '新增客户',
                        type: 'line',
                        stack: '总量',
                        data: Config.statistics.userlist
                    },
                    {
                        name: '订单转化率',
                        type: 'line',
                        stack: '总量',
                        data: Config.statistics.convertlist
                    },
                ]
            };
            myChart.setOption(option);
            myChart_uv.setOption(option_uv);
            myChart_pay.setOption(option_pay);
            myChart_user.setOption(option_user);
            $(window).resize(function () {
                myChart.resize();

            });
            $(document).on("click", ".btn-checkversion", function () {
                top.window.$("[data-toggle=checkupdate]").trigger("click");
            });
            $(document).on("click", ".btn-refresh", function () {
                setTimeout(function () {
                    myChart.resize();
                }, 0);
            });
            $(document).on("click", ".btn-refresh", function () {
                $(".datetimerange").trigger("blur");
            });
            Form.api.bindevent($("form[role=form]"));
            $(document).on("click", ".btn-filter", function () {
                var label = $(this).text();
                var obj = $(".datetimerange").data("daterangepicker");

                var dates = obj.ranges[label];
                obj.startDate = dates[0];
                obj.endDate = dates[1];

                obj.clickApply();
            });
            $(".datetimerange").on("blur", function () {
                Fast.api.ajax({
                    url: 'wanlshop/console/index',
                    data: {date: $(this).val()}
                }, function (data) {

                    var option = {

                        tooltip: {
                            trigger: 'axis'
                        },
                        legend: {
                            data: [__('Sales'), __('Orders')]
                        },
                        /*toolbox: {
                            show: true,
                            feature: {
                                dataView: {show: true, readOnly: false},
                                magicType: {show: true, type: ['line', 'bar']},
                                restore: {show: true},
                                saveAsImage: {show: true}
                            }
                        },*/
                        calculable: true,
                        xAxis: [
                            {
                                type: 'category',
                                data: data.columns
                            }
                        ],
                        yAxis: [
                            {
                                type: 'value'
                            }
                        ],
                        series: [
                            {
                                name: __('Sales'),
                                type: 'bar',
                                data: data.paylist,
                                markPoint: {
                                    data: [
                                        {type: 'max', name: '最大值'},
                                        {type: 'min', name: '最小值'}
                                    ]
                                },

                            },
                            {
                                name: __('Orders'),
                                type: 'bar',
                                data: data.createlist,

                            }
                        ]
                    };
                    var option_uv = {

                        tooltip: {
                            trigger: 'item'
                        },
                        /*legend: {
                            orient: 'vertical',
                            left: 'center',
                        },*/
                        series: [
                            {
                                name: '访问来源',
                                type: 'pie',
                                radius: '50%',
                                data: [
                                    {value: data.uv.pc_uv, name: 'PC'},
                                    {value: data.uv.wap_uv, name: 'WAP'},

                                ],
                                emphasis: {
                                    itemStyle: {
                                        shadowBlur: 10,
                                        shadowOffsetX: 0,
                                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                                    }
                                }
                            }
                        ]
                    };
                    var option_pay = {
                        title: {text: '', subtext: ''},
                        tooltip: {trigger: 'axis'},
                        legend: {data: ['订单数量','订单金额']},
                        toolbox: {
                            show: false,
                            feature: {magicType: {show: true, type: ['stack', 'tiled']}, saveAsImage: {show: true}}
                        },
                        xAxis: {
                            type: 'category',
                            boundaryGap: false,
                            data: data.columns
                        },
                        yAxis: {},
                        grid: [{left: 'left', top: 'top', right: '10', bottom: 30}],
                        series: [{
                            name: '订单数量',
                            type: 'line',
                            smooth: true,
                            areaStyle: {normal: {}},
                            lineStyle: {normal: {width: 1.5}},
                            data: data.createlist
                        }, {
                            name: '订单金额',
                            type: 'line',
                            smooth: true,
                            areaStyle: {normal: {}},
                            lineStyle: {normal: {width: 1.5}},
                            data: data.orderlist
                        }]
                    };
                    var option_user = {

                        tooltip: {
                            trigger: 'axis'
                        },
                        legend: {
                            data: ['新增客户', '订单转化率']
                        },
                        grid: {
                            left: '3%',
                            right: '4%',
                            bottom: '3%',
                            containLabel: true
                        },
                        toolbox: {
                            feature: {
                                saveAsImage: {}
                            }
                        },
                        xAxis: {
                            type: 'category',
                            boundaryGap: false,
                            data: data.columns
                        },
                        yAxis: {
                            type: 'value'
                        },
                        series: [
                            {
                                name: '新增客户',
                                type: 'line',
                                stack: '总量',
                                data: data.userlist
                            },
                            {
                                name: '订单转化率',
                                type: 'line',
                                stack: '总量',
                                data: data.convertlist
                            },
                        ]
                    };
                    myChart.setOption(option);
                    myChart_uv.setOption(option_uv);
                    myChart_pay.setOption(option_pay);
                    myChart_user.setOption(option_user);
                    $('#sum_uv').text(data.uv.sum_uv)
                    $('#pc_uv').text(data.uv.sum_uv ? ((data.uv.pc_uv/data.uv.sum_uv) *100).toFixed(2) + '%' : '0%')
                    $('#wap_uv').text(data.uv.sum_uv ? ((data.uv.wap_uv/data.uv.sum_uv) *100 ).toFixed(2)+ '%' : '0%')
                    $('#pay_sum').text(data.order.pay_sum)
                    return false;
                });
            });
            //每隔一分钟定时刷新图表
            setInterval(function () {
                $(".btn-refresh").trigger("click");
            }, 60000);
            $('#sum_uv').text(Config.statistics.uv.sum_uv)
            $('#pc_uv').text(Config.statistics.uv.sum_uv ? ((Config.statistics.uv.pc_uv/Config.statistics.uv.sum_uv)*100).toFixed(2)  + '%' : '0%')
            $('#wap_uv').text(Config.statistics.uv.sum_uv ? ((Config.statistics.uv.wap_uv/Config.statistics.uv.sum_uv) *100).toFixed(2) + '%' : '0%')
            $('#pay_sum').text(Config.statistics.order.pay_sum)
        }
    };
    return Controller;
});